package com.cy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@RequestMapping("/consumer/template/")
public class RestConsumerController {

    @Autowired
    private RestTemplate loadBalancedRestTemplate;

    // http://localhost:8090/consumer/template/93
    @DeleteMapping("{id}")
    public String doDelete(@PathVariable Integer id){
        /*String url = String.format("http://%s/provider/template/%s",
                "nacos-provider",id);*/
        String url = "http://nacos-provider/provider/template/"+id;
        loadBalancedRestTemplate.delete(url);
        return "delete ok";
    }

    // http://localhost:8090/consumer/template
    @PostMapping
    public Map<String,Object>  doPost(
            @RequestBody Map<String,Object> map){
        //定义服务提供方的url
        /*String url = String.format("http://%s/provider/template/",
                                    "nacos-provider");*/
        String url = "http://nacos-provider/provider/template/";
        return loadBalancedRestTemplate.postForObject(url,
                map,
                Map.class);
    }

    // http://localhost:8090/consumer/template
    @PutMapping
    public String  doPut(
            @RequestBody Map<String,Object> map){
        //定义服务提供方的url
        /*String url = String.format("http://%s/provider/template/",
                "nacos-provider");*/
        String url = "http://nacos-provider/provider/template/";
        loadBalancedRestTemplate.put(url,map);

        return "put ok";
    }
}
